<template>
	<view style="height: 100%; display: flex; flex-direction: column">
		<view>
			<view style="background-color: darkorange;color:#f1f1f1;text-align:center; padding: 12px 0; ">购物车</view>
			<view class="flex flex-col page-body-tab relative" style="padding-top: 5rpx;">
				<view class="goods-item ">
					<view>
						
						<u-checkbox-group v-model="checkboxValue1" placement="column"
							@change="checkboxChange">
							<view class="info " v-for="(item,index) in shohppingList" :key="index"
								@click="goDetial(item.id)">
								<view style="height: 20rpx;"></view>
								<view
									style="height: 210rpx;background-color: #f1f1f1;padding-top: 20rpx;border-radius: 12px;">
									<view style=" padding-top: 75rpx;padding-left: 15rpx;">
											<u-checkbox :customStyle="{marginBottom: '2px'}"  :label="t" :name="item.id">
											</u-checkbox>
									</view>
							
									<view style="color:dimgray; margin-top: -90rpx;padding-left: 220rpx;">
										{{item.commodityName}}
										{{item.commoditySpec}}
									</view>
									<view style="padding-left: 70rpx;margin-top: -40rpx;">
										<u--image :src="imgUrl + item.commodityImageUrl" width="140rpx" mode="aspectFit"
											height="140rpx">
										</u--image>
									</view>
									<view style="padding-left: 210rpx;margin-top: -50rpx;color: ;color: crimson;">
										￥{{item.commodityPrice}} </view>
									<view style="margin-top: -50rpx;padding-left: 65%">
										<u-number-box v-model="value" @change="valChange"></u-number-box>
									</view>
								</view>
							</view>
						</u-checkbox-group>
						
						<view class="foot-bar absolute flex j-s "
							style=" position: fixed;margin-bottom: 120rpx;margin-left:-11rpx ;">
							<view class="flex a-c left">
								<view style="margin-top: 16rpx;">
									<u-checkbox-group v-model="checkboxValue2" placement="column"
										@change="allChange">
										<u-checkbox :label="t" name="全选">
										</u-checkbox>
									</u-checkbox-group>
								</view>
								<!-- 	<u-icon class='icon c-m' name="checkmark-circle"></u-icon> -->
								<view class="label">
									全选
								</view>
							</view>
							<view class="flex right a-c">
								<view>总计:</view>
								<view class="c-m total-price">{{totalPrice}}</view>
								<view class="buy flex-c" @click=payNow>立即支付</view>

							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
<u-toast ref="uToast"></u-toast>


		<!-- 底部导航栏 -->
		<view style="width: 100%; flex: none; position: fixed;bottom: 0px;">
			<c-tabbar></c-tabbar>
		</view>
	</view>
</template>

<script>
	import api from './api.js'
	export default {
		data() {
			return {
				value: 0,
				t:'',
				totalPrice:0.00,
				baseList: [],
				shohppingList: [],
				commodityDetails: [],
				checkboxValue1:[],
				checkboxValue2:[],
				imgUrl: api.FileById,
				// 基本案列数据
				checkboxList1: [{

					name: '',
					disabled: false
				}],
				 allCheck: {
				                    name: '全选',
				                    values: 'all',
				                    checked: true
				                },
								
			}
		},
		onShow() {
			this.searchType()
		},
		methods: {
			valChange(e) {
				console.log('当前值为: ' + e.value)
			},
			checkboxChange(n) {
				this.totalPrice = 0
				if(n.length == this.shohppingList.length){
					this.checkboxValue2 = ['全选']
				}else{
					this.checkboxValue2 = ['']
				}
				n.forEach(item=>{
					let commint = this.shohppingList.find(x => x.id == item)
					this.totalPrice+=commint.count * commint.commodityPrice
				})
			},
			async searchType() {
				var result = await api.GetCommodityType()
				this.shohppingList = result.data.datas
				console.log(result);
			},
			allChange(n){
				console.log(n);
				this.totalPrice = 0
				if(n == ''){
					this.checkboxValue1 = []
					
				}else{
					this.shohppingList.forEach(item=>{
						this.totalPrice+=item.count * item.commodityPrice
						this.checkboxValue1.push(item.id)
					})
				}
			},payNow(){
				this.checkboxValue1.forEach(item=>{
				let commodity = this.shohppingList.find(x => x.id == item)
				    api.Pay({CommodityCount:commodity.count,commodityId:commodity.commodityId}).then(res=>{
						if(res.data.message == '新增成功'){
							api.deleteCommodity(item)
							this.shohppingList = this.shohppingList.filter(x=>x.id != item)
							this.$refs.uToast.show({
								type:"success",
								message:"付款成功！"
							})
							this.totalPrice -= commodity.count * commodity.commodityPrice
						}
					})
				})
				console.log('payNow')
			}
		}
	}

	// async searchType() {
	// 	var result = await api.GetCommodityType()
	// 	this.shohppingList = result.data.datas
	// 	console.log(result);
	// }
</script>

<style lang="scss">
	.goods-item {
		padding: 16rpx;

		.icon {
			font-size: 50rpx;
			padding: 0 20rpx;
		}

		image {
			height: 160rpx;
			width: 180rpx;
			border-radius: 10rpx;
		}

		.info {

			flex: 1;
			padding: 0 16rpx;
		}

		.title {
			line-height: 30rpx;
			max-height: 40rpx;
			display: -webkit-box;
			-webkit-box-orient: vertical;
			-webkit-line-clamp: 2;
			overflow: hidden;
			word-break: break-all;
		}
	}

	.j-c {
		justify-content: center;
	}

	.j-s {
		justify-content: space-between;
	}

	.a-c {
		align-items: center;
	}

	.flex-col {
		flex-direction: column;
	}

	.flex-c {
		height: 100px;
		line-height: 50px;
	}

	.a-s {
		align-content: space-between;
	}

	.c-m {
		color: #ff0000;
	}

	.flex {
		display: flex;
		flex-wrap: wrap;
	}

	.biaoti {
		font-size: 36rpx;
		color: #004ade;
	}

	.foot-bar {
		height: 100rpx;
		width: 100%;
		bottom: 0px;
		background: #fff;
		box-shadow: 0 -1rpx 2rpx #ccc;
		z-index: 9999;
	}

	.left {
		padding: 0 20rpx;
		font-weight: bold;
	}

	.label {
		font-size: 34rpx;
	}

	.icon {
		padding: 0 10rpx;
	}

	.right {
		height: 100%;

		.total-price {
			padding: 0 20rpx;

		}
	}

	.buy {
		color: #fff;
		padding: 0 40rpx;
		background: #fda900;
		height: 100%;


	}




	.total-price {
		font-size: 34rpx;
	}

	.up {
		color: #fff;
		text-align: center;
		height: 44px;
		line-height: 80rpx;
		background-color: #ffc850;
	}
</style>
